//
//  PhotoBrowerAnimater.swift
//  Weibo
//
//  Created by 孙明海 on 16/2/20.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit
//MARK: - 展现动画的协议
/// present
protocol PhotoBrowerPresentDelegate:NSObjectProtocol {
    /// 指定 indexPath 对应的 imageView 做动画效果
    func imageViewForPresent(indexPath: NSIndexPath) -> UIImageView
    /// 转场动画的起始位置
    func photoBrowerPresentFromRect(indexPath: NSIndexPath) -> CGRect
    /// 转场动画的目标位置
    func photoBrowerPresentToRect(indexPath: NSIndexPath) -> CGRect
}
//MARK: - 解除动画的协议
protocol PhotoBrowerDismissDelegate: NSObjectProtocol {
    /// 解除动画的图像视图 (包含起始位置)
    func imageViewForDismiss() -> UIImageView
    /// 解除动画的索引
    func indexPathForDismiss() -> NSIndexPath
}

class PhotoBrowerAnimater: NSObject, UIViewControllerTransitioningDelegate {
    /// present 的代理
    private weak var photoBrowerPresentDelegate: PhotoBrowerPresentDelegate?
    private weak var photoBrowerDismissDelegate: PhotoBrowerDismissDelegate?
    /// 动画图片索引
    private var indexPath: NSIndexPath?
    
    func setDelegateParmas(photoBrowerPresentDelegate: PhotoBrowerPresentDelegate,photoBrowerDismissDelegate:PhotoBrowerDismissDelegate, indexPath: NSIndexPath) {
        self.photoBrowerPresentDelegate = photoBrowerPresentDelegate
        self.photoBrowerDismissDelegate = photoBrowerDismissDelegate
        self.indexPath = indexPath
    }
    
    /// 是否 present 的标识
    private var isPresent: Bool = false
    /// 提供 modal 展现的 动画对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    /// 提供 dismiss 的 动画对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
}

extension PhotoBrowerAnimater: UIViewControllerAnimatedTransitioning {
    // 动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    /// 实现动画的具体效果 - 一旦实现了这个方法,所有的动画代理交由程序员
    ///
    /// - parameter transitionContext: 转场动画的上下文 - 提供转场动画的素材
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresent ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)
    }
    /// dismiss
    ///
    /// - parameter transitionContext: 转场上下文
    private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let photoBrowerPresentDelegate = photoBrowerPresentDelegate, photoBrowerDismissDelegate = photoBrowerDismissDelegate else {
            return
        }
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        fromView?.removeFromSuperview()
        
        let imageView = photoBrowerDismissDelegate.imageViewForDismiss()
        transitionContext.containerView()?.addSubview(imageView)
        
        let indexPath = photoBrowerDismissDelegate.indexPathForDismiss()
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            imageView.frame = photoBrowerPresentDelegate.photoBrowerPresentFromRect(indexPath)
            }) { (_) -> Void in
                imageView.removeFromSuperview()
                transitionContext.completeTransition(true)
        }
    }
    
    /// modal
    ///
    /// - parameter transitionContext: 转场动画的上下文
    private func presentAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let indexPath = indexPath, photoBrowerPresentDelegate = photoBrowerPresentDelegate else {
            return
        }
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        transitionContext.containerView()?.addSubview(toView)
        toView.alpha = 0
        let imageView = photoBrowerPresentDelegate.imageViewForPresent(indexPath)
        imageView.frame = photoBrowerPresentDelegate.photoBrowerPresentFromRect(indexPath)
        transitionContext.containerView()?.addSubview(imageView)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
             toView.alpha = 1
            imageView.frame = photoBrowerPresentDelegate.photoBrowerPresentToRect(indexPath)
            }) { (_) -> Void in
               
                imageView.removeFromSuperview()
                
                /// 告诉系统转场动画完成
                transitionContext.completeTransition(true)
                
        }
    }
}